package com.nut.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;


/**
 * @ author : HyoJoo-W
 * @ createDate : 2021-05-09 15:31
 * @ Description : UserDetailsService实现类,自定义service
 */
@Service
public class UserServiceImpl implements UserDetailsService
{
    @Autowired
    private PasswordEncoder pw;

    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException
    {
        //1 根据username查询数据库
        if (!"admin".equals(s))
        {
            throw new UsernameNotFoundException("用户名或密码错误");
        }
        //2 根据查询的对象比较密码
        String password = pw.encode("123456");

        //3 返回比较对象
        return new User(
                "admin",
                password,
                AuthorityUtils.commaSeparatedStringToAuthorityList("admin,normal,ROLE_abc"));
    }
}
